<?php

/**
 * @author Murat Cem YALIN <muratcem@gmail.com>
 * @copyright 2014 muratcem.com - walkerPHP.com
 * @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3
 * @version 1.0.1
 */

namespace core\Model;

use \core\Db\Db;

class Model implements IModel{

    /**
     * to create and empty model with fields ready
     * i think this will only work on mySQL
     *
     * @param bool $getFieldsFromTable
     */
    function __construct($getFieldsFromTable = FALSE){
        if ($getFieldsFromTable){
            $vars = get_class_vars(get_class($this));
            if (isset($vars['table'])){
                $result = Db::Query('SHOW FIELDS FROM '.$this->table);
                if (count($result)){
                    foreach($result as $value){
                        $this->{$value['Field']} = '';
                    }
                }
            }
        }
    }

    /**
     * magic __get method to get the value of a variable to class
     * @param $name
     * @return mixed
     */
    function __get($name){
        return isset($this->{$name}) ? $this->{$name} : FALSE;
    }

    /**
     * magic __set method to set a variable and a value to class
     *
     * @param $name
     * @param $value
     * @return $this
     */
    function __set($name, $value){
        $this->{$name} = $value;
        return $this;
    }

    /**
     * fetching records from table of model
     *
     * @param string $orderBy
     * @param array $filter
     * @return array
     */
    public function fetchAll($orderBy = '', $filter = array()){
        if (is_array($filter) && count($filter) > 1){
            switch(count($filter)){
                case 2:
                    $sign = '=';
                    $value = $filter[1];
                    break;
                case 3:
                    $sign = $filter[1];
                    $value = $filter[2];
                    break;
                default:
                    $sign = '';
                    $value = '';
                    break;
            }
            return Db::table($this->table)
                ->where($filter[0], $sign, $value)
                ->order_by($orderBy)
                ->get();
        } else {
            return Db::table($this->table)
                ->order_by($orderBy)
                ->get();
        }
    }

    /**
     * find records from the table of model
     * similar to fetchAll returns array on multiple result
     * except returns $this on single record
     *
     * @return $this|array
     */
    public function find(){
        $args     = func_get_args();
        $result   = array();

        switch(count($args)){
            case 1:
                $result = Db::table($this->table)
                    ->where('id', '=', intval($args[0]))
                    ->get();
                break;
            case 2:
                $result = Db::table($this->table)
                    ->where($args[0], '=', $args[1])
                    ->get();
                break;
            case 3:
                $result = Db::table($this->table)
                    ->where($args[0],$args[1], $args[2])
                    ->get();
                break;
        }
        if ($result !== FALSE && count($result)){
            if (count($result) == 1){
                foreach($result[0] as $key => $value){
                    $this->{$key} = $value;
                }
                return $this;
            } else {
                return $result;
            }
        } else {
            return $this;
        }
    }

    /**
     * try to save new record by using class variables
     * id variable will set with PDO::lastInsertId()
     *
     * @return bool
     */
    public function save(){
        $varArray = array();
        foreach($this as $key => $value){
            if ($key != 'table'){
                $varArray[$key] = $this->{$key};
            }
        }
        $result = Db::table($this->table)->insert($varArray);
        if ($result !== FALSE){
            $this->id = Db::getLastInsertId();
        }
        return $result;
    }

    /**
     * try to update the record in table by using class variables
     * where clauses will be created with $$updateFieldBy
     *
     * @param string $updateFieldBy
     * @return bool
     */
    public function update($updateFieldBy = ''){
        $varArray = array();
        foreach($this as $key => $value){
            if ($key != 'table'){
                $varArray[$key] = $this->{$key};
            }
        }
        return Db::table($this->table)
            ->where($updateFieldBy, '=', $this->{$updateFieldBy})
            ->update($varArray);
    }

    /**
     * try to delete record in table by ysing $deleteFieldBy fieldname
     * and its value
     *
     * @param string $deleteFieldBy
     * @return mixed
     */
    public function delete($deleteFieldBy = ''){
        return Db::table($this->table)
            ->where($deleteFieldBy, '=', $this->{$deleteFieldBy})
            ->delete();
    }



}
































